<?php

namespace app\models;

use yii;
use yii\db\Query;

/**
 *报销表
 *
 */
class Baoxiao extends BaseModel
{

    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix . 'baoxiao';
    }

    public function one($where = [])
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['id']) && $where['id']!==0) {
            $db->andWhere(['id' => $where['id']]);
        }
        return $db->limit(1)->one();
    }

    public function all($where = [], $offset = false, $limit = false, $order = false)
    {
        $db = new Query();
        $db->select('*')->from($this->table);

        if (!empty($where['job_number'])) {
            if (is_array($where['job_number'])) {
                $db->andWhere(['in', 'job_number', $where['job_number']]);
            } else {
                $db->andWhere(['job_number' => $where['job_number']]);
            }
        }
        if (!empty($where['status'])) {
            $db->andwhere(['status' => $where['status']]);
        }
        if (!empty($where['type'])) {
            $db->andWhere(['type' => $where['type']]);
        }
        if (!empty($where['type2'])) {
            $db->andWhere(['type2' => $where['type2']]);
        }
        if (!empty($where['invoice'])) {
            $db->andWhere(['invoice' => $where['invoice']]);
        }
        if (!empty($where['current_verify'])) {
            $db->andWhere(['current_verify' => $where['current_verify']]);
        }
        if (!empty($where['begin_date'])) {
            $db->andWhere(['>=', 'create_at', $where['begin_date']]);
        }
        if (!empty($where['end_date'])) {
            $db->andWhere(['<=', 'create_at', $where['end_date']]);
        }
        if ($order) {
            $db->orderBy($order);
        }
        if ($offset) {
            $db->offset($offset);
        }
        if ($limit) {
            $db->limit($limit);
        }

        $list = $db->all();

        return $list;
    }

    /**报销列表size
     * @param array $where
     * @return int
     */
    public function get_baoxiao_size($where = [])
    {
        $db = new Query();
        $db->select('count(id) as num')->from($this->table);
        if (!empty($where['job_number'])) {
            if (is_array($where['job_number'])) {
                $db->andWhere(['in', 'job_number', $where['job_number']]);
            } else {
                $db->andWhere(['job_number' => $where['job_number']]);
            }
        }
        if (!empty($where['status'])) {
            $db->andwhere(['status' => $where['status']]);
        }
        if (!empty($where['type'])) {
            $db->andWhere(['type' => $where['type']]);
        }
        if (!empty($where['type2'])) {
            $db->andWhere(['type2' => $where['type2']]);
        }
        if (!empty($where['invoice'])) {
            $db->andWhere(['invoice' => $where['invoice']]);
        }
        if (!empty($where['current_verify'])) {
            $db->andWhere(['current_verify' => $where['current_verify']]);
        }
        if (!empty($where['begin_date'])) {
            $db->andWhere(['>=', 'create_at', $where['begin_date']]);
        }
        if (!empty($where['end_date'])) {
            $db->andWhere(['<=', 'create_at', $where['end_date']]);
        }
        $info = $db->one();
        return $info ? $info['num'] : 0;
    }

    public function caculateData($where = [])
    {
        $db = new Query();
        $db->select('sum(fee) as money')->from($this->table);
        if (!empty($where['job_number'])) {
            if (is_array($where['job_number'])) {
                $db->andWhere(['in', 'job_number', $where['job_number']]);
            } else {
                $db->andWhere(['job_number' => $where['job_number']]);
            }
        }
        if (!empty($where['status'])) {
            $db->andwhere(['status' => $where['status']]);
        }
        if (!empty($where['type'])) {
            $db->andWhere(['type' => $where['type']]);
        }
        if (!empty($where['type2'])) {
            $db->andWhere(['type2' => $where['type2']]);
        }
        if (!empty($where['invoice'])) {
            $db->andWhere(['invoice' => $where['invoice']]);
        }
        if (!empty($where['current_verify'])) {
            $db->andWhere(['current_verify' => $where['current_verify']]);
        }
        if (!empty($where['begin_date'])) {
            $db->andWhere(['>=', 'create_at', $where['begin_date']]);
        }
        if (!empty($where['end_date'])) {
            $db->andWhere(['<=', 'create_at', $where['end_date']]);
        }
        $info = $db->one();
        return $info ? $info['money'] : 0;
    }

    /**
     * @param $in
     * @return array
     */
    public function getListByParam($in)
    {
        $db = new Query();
        $list = $db->select('*')->from($this->table)->where(['in', 'invoice', $in])->all();
        $tmp = [];
        foreach ($list as $v) {
            $info = json_decode($v['detail'], true);
            if (isset($tmp[$v['invoice']])) {
                $tmp[$v['invoice']] += $info['fee'];
            } else {
                $tmp[$v['invoice']] = $info['fee'];
            }
        }
        return $tmp;
    }


    //检测未报销
    public function check_baoxiao()
    {
        $db = new Query();
        $list = $db->select('count(id) as num')
            ->from($this->table)
            ->where(['status' => 0, 'current_verify' => 2])
            ->one();
        return $list;
    }


    /**通过invoice获取报销信息
     * @param $in
     * @return array
     */
    public function getBaoxiaoByInvoice($in)
    {
        $db = new Query();
        $list = $db->select('*')->from($this->table)->where(['in', 'invoice', $in])->all();
        $arr = [];
        foreach ($list as $k => $v) {
            $tmp = json_decode($v['detail'], true);
            if (!isset($arr[$v['invoice']])) {
                $arr[$v['invoice']] = $tmp['fee'];
            } else {
                $arr[$v['invoice']] += $tmp['fee'];
            }
        }
        return $arr;
    }

    /**计算未付款金额
     * @return int
     */
    public function sumMoney($where)
    {
        $db = new Query();
        $db->select('sum(fee) as num')->from($this->table);
        $db->andWhere(['>', 'time', 0])
            ->andWhere(['pay_time' => 0]);
        if (!empty($where['job_number'])) {
            if (is_array($where['job_number'])) {
                $db->andWhere(['in', 'job_number', $where['job_number']]);
            } else {
                $db->andWhere(['job_number' => $where['job_number']]);
            }
        }
        if (!empty($where['status'])) {
            $db->andwhere(['status' => $where['status']]);
        }
        if (!empty($where['type'])) {
            $db->andWhere(['type' => $where['type']]);
        }
        if (!empty($where['type2'])) {
            $db->andWhere(['type2' => $where['type2']]);
        }
        if (!empty($where['invoice'])) {
            $db->andWhere(['invoice' => $where['invoice']]);
        }
        $re = $db->andWhere(['>=', 'create_at', date('Y-m-d', time() - 60 * 24 * 60 * 60)])
            ->one();
        return $re ? $re['num'] : 0;
    }
}
